#!/bin/bash

if GIT_ROOT=$(git rev-parse --show-toplevel 2>/dev/null); then
    TOP_DIR="$GIT_ROOT"
else
    SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
    TOP_DIR="$SCRIPT_DIR"
fi

export ZSH="$TOP_DIR"
WORK_DIR="${ZSH}/scripts"

cmd_start() {
  # 确保工作目录存在（避免后续操作报错）
  mkdir -p "${WORK_DIR}" 2>/dev/null
  echo "工作目录: $WORK_DIR"
}

cmd_complete() {
 return 0
}

# 系统专属操作函数（直接接收并使用传递的参数）
cmd_ubuntu() {
  echo "1. 安装必要软件"
  sudo add-apt-repository ppa:deadsnakes/ppa
  sudo apt-get update

  sudo apt-get install -y software-properties-common
  sudo apt-get install -y zsh wget curl
  sudo apt-get install -y net-tools terminator
  sudo apt-get install -y vim gedit
  sudo apt-get install -y maven
  sudo apt-get install -y nodejs npm
  sudo apt-get install -y autojump
  sudo apt-get install -y docker
  sudo apt-get install -y openssh-client
  sudo apt-get install -y nginx git
  sudo apt-get install -y zsh neofetch
  sudo apt-get --only-upgrade install python3
  sudo apt-get install -y python3.13
  sudo apt-get install -y python3-pip
  sudo apt-get install -y build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget

  # 统一 python 版本
  sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.13 1
  # 添加Python 3.10到 alternatives（如果需要保留切换选项）
  sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 2
  # 配置默认Python版本（选择你希望的版本编号）
  sudo echo 1 | update-alternatives --config python
  # 统一 pip 版本
  # 为pip设置 alternatives
  sudo update-alternatives --install /usr/bin/pip pip /usr/bin/pip3.13 1
  sudo update-alternatives --install /usr/bin/pip pip /usr/bin/pip3.10 2
  # 配置默认pip版本
  sudo echo 1 | update-alternatives --config pip
  # 检查输出
  python --version
  python3 --version
  pip --version
  pip3 --version

  pip install pip -U
  pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

  curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
  export NVM_DIR="$HOME/.nvm"
  [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
  [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"

  nvm install 20
  nvm use 20
  npm install -g npm@10.9.2
#  nodejs -v
  node -v
  npm -v

# 安装 go
  # 1. 移除旧版本（可选，避免冲突）
  sudo apt remove golang-go
  # 2. 添加 Go 官方源（支持 1.21+ 版本）
  sudo add-apt-repository ppa:longsleep/golang-backports
  sudo apt update
  # 3. 安装 Go 1.24（或指定 1.21+ 版本，如 1.21、1.23）
  sudo apt install golang-1.24-go

  echo "2. 更新和清理软件"
  #sudo add-apt-repository ppa:deadsnakes/ppa
  #sudo apt-get upgrades
  sudo apt-get autoremove
  sudo apt-get autoclean

  echo "3. 更新配置文件"
  cp -r "$ZSH"/configs/.vimrc "$HOME"/.vimrc
  cp -r "$ZSH"/configs/.zshrc "$HOME"/.zshrc

  echo "4. 切换终端为zsh"
  sudo chsh -s /bin/zsh
  zsh
}

cmd_debian() {
  echo "1. 安装必要软件 (Debian)"
  sudo apt-get update
  sudo apt-get install -y software-properties-common
  sudo apt-get install -y zsh wget curl
  sudo apt-get install -y net-tools terminator
  sudo apt-get install -y vim
  sudo apt-get install -y git
  sudo apt-get install -y python3 python3-pip

  echo "2. 更新和清理软件"
  sudo apt-get autoremove -y
  sudo apt-get autoclean -y

  echo "3. 更新配置文件"
  cp -r "$ZSH"/configs/.vimrc "$HOME"/.vimrc
  cp -r "$ZSH"/configs/.zshrc "$HOME"/.zshrc

  echo "4. 切换终端为zsh"
  sudo chsh -s /bin/zsh
  zsh
}

cmd_centos() {
  echo "1. 安装必要软件 (CentOS/RHEL)"
  if command -v dnf >/dev/null 2>&1; then
    PKG_MGR=dnf
  else
    PKG_MGR=yum
  fi

  sudo $PKG_MGR -y install zsh wget curl git vim net-tools

  echo "2. 更新和清理软件"
  sudo $PKG_MGR -y autoremove || true
  sudo $PKG_MGR -y clean all || true

  echo "3. 更新配置文件"
  cp -r "$ZSH"/configs/.vimrc "$HOME"/.vimrc
  cp -r "$ZSH"/configs/.zshrc "$HOME"/.zshrc

  echo "4. 切换终端为zsh"
  sudo chsh -s /bin/zsh
  zsh
}

cmd_macos() {
  echo "1. 安装必要软件"
  /bin/bash "$ZSH"/tools/install.sh

  brew install git
  brew install maven
  brew install nodejs
  brew install npm

  brew install nginx
  brew install CocoaPods
  brew install qrencode
  brew install chromium
  brew install xcode
  brew install scrcpy
  brew install mongodb-atlas
  brew install neofetch

  brew install python@3.13
  pip3 install -y myqr

  brew install pyenv

  xcode-select --install
  curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
  sudo chown -R "$USER":"$USER" ~/.npm
  source "$HOME"/.zshrc
  nvm install 22.17.1
  nvm use 22.17.1
#  nodejs -v
  node -v
  npm -v

  #brew tap dart-lang/dart
  #brew install dart
  #brew upgrade dart

  echo "2. 更新和清理软件"
  brew update

  echo "3. 更新配置文件"
  cp -r "$ZSH"/configs/.vimrc "$HOME"/.vimrc
  cp -r "$ZSH"/configs/.zshrc "$HOME"/.zshrc

  echo "4. 切换终端为zsh"
  chsh -s /bin/zsh
}

cmd_windows() {
  echo "Windows系统操作，接收参数: $*"
  # 参数使用示例
}

cmd_wsl() {
  echo "WSL系统操作，接收参数: $*"
  # 参数使用示例
}

cmd_unknown() {
  echo "未知系统操作，接收参数: $*"
  # 参数使用示例
}

source "$WORK_DIR"/common_os.sh || . "$WORK_DIR"/common_os.sh

# 启动主程序并传递所有参数
main "$@"
